<template>
    <div class="goods-list">
        <!-- <router-link class="goods-item" v-for="item in goodslists" :key="item.id" :to="'/home/goodsinfo/' + item.id">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩余{{item.stock_quantity}}件</span>
                </p>
            </div>
        </router-link> -->
        <div class="goods-item" v-for="item in goodslists" :key="item.id" @click="godatail(item.id)">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩余{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>
        <mt-button id="tc" type="danger" size="large" plain @click="addpageindex()">更多商品</mt-button>
    </div>
</template>

<script>
import { Toast } from 'mint-ui'
export default {
    data(){
        return{
            pageindex:1,//当前页数
            goodslists:[]//商品数据
        }
    },
    created(){
        this.getgoodslists();
    },
    methods:{
        getgoodslists(){
            this.$http.get("http://www.liulongbin.top:3005/api/getgoods?pageindex=" + this.pageindex).then(result=>{
                if(result.body.status === 0){
                    this.goodslists = this.goodslists.concat(result.body.message);
                }
            })
        },
        addpageindex(){
            if (this.pageindex >= 2) {
               return Toast({
                                message: '暂无更多商品',
                                position: 'content',
                                duration: 800
                        });
            }else{
                 this.pageindex++;
                this.getgoodslists();
            }
        },
        godatail(id){
            // 通过 JS 方式进行路由
            // 注意：一定要区分 this.$router 和 this.$route 这两个对象，
            // 其中: this.$route 是路由[参数对象]，所有路由中的参数，params，query 都属于它
            // 其中：this.$router 是一个路由[导航对象]，用它 可以方便的使用 JS 代码，实现路由的前进，后退，跳转到新的 URL 地址 
            // 字符串
            //router.push('home')

            // 对象
            // router.push({ path: 'home' })

            // 命名的路由
            //router.push({ name: 'user', params: { userId: '123' }})

            // 带查询参数，变成 /register?plan=private
            //router.push({ path: 'register', query: { plan: 'private' }})
            this.$router.push({ name: "goodsinfo" ,params:{ id } });
        }
    }
}
</script>

<style lang="scss" scoped>
    .goods-list{
        display:flex;
        flex-wrap: wrap;
        justify-content: space-between;
        padding: 7px;
        .goods-item{
            border: 1px solid #ccc;
            box-shadow: 0 0 8px #ccc;
            width: 49%;
            margin: 4px 0px;
            padding: 2px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            min-height: 293px;
            img{
                width: 100%;
                min-height: 164.52px;
            }
            .title{
                font-size: 14px;
            }

            .info{
                background: #eee;
                p{
                    margin: 0;
                    padding: 4px;
                }
                .price{
                    .now{
                        color: red;
                        font-weight: bold;
                        font-size: 16px;
                    }
                    .old{
                        text-decoration: line-through;
                        font-size: 12px;
                        margin-left: 10px;
                    }
                }
                .sell{
                    display: flex;
                    justify-content: space-between;
                    font-size: 13px;
                }
            }
        }
    }

</style>
